<?php

	// Session
	session_start();
	
	// If the user its not logged
	if (!isset($_SESSION['user_id'])) {
		header("Location: login.php");
		exit;
	}
	
	
	// If the Form its Submitit with NULL options
	if ($_POST['checkbox'] == NULL) {

		// Redirect the page with EMPTY message
		header('Location: books.php?splash=empty');
		exit;
	}

	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	// Project
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/members/dropbooks.html');
	define('UID',$_SESSION['user_id']);
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	
	
	// Connection
	dbOpen("comunity");
	
	
	// Inicializimi i shablonave
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../members"));
	$tmp->setContent("MYMENU",$tmp->menu("mymenu","books.php"));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	
	
	// INDEXING OPTIONS
	$SECTION = indexing("SELECT id, name FROM sections");
	$ACCESS = $tmp->indexing("access");


	// If the user its agree to drop the books
	if ($_POST['dropbooks'] == 1) {
	
		// Fshirja e titujve nga arkiva library
		foreach ($_POST['checkbox'] as $key) {
			mysql_query("DELETE FROM library WHERE id = '".$key."'");
		}
		
		// Fshirja e permbatjeve nga arkiva content
		foreach ($_POST['checkbox'] as $key) {
			mysql_query("DELETE FROM contents WHERE book_id = '".$key."'");
		}
		
		// Fshirja e komenteve nga arkiva comment
		foreach ($_POST['checkbox'] as $key) {
			mysql_query("DELETE FROM comments WHERE book_id = '".$key."'");
		}
		
		// Fshirja e vleres nga arkiva ratin
		foreach ($_POST['checkbox'] as $key) {
			mysql_query("DELETE FROM ratings WHERE book_id = '".$key."'");
		}
		
		// Kalo ne listimin e librave dhe trego porosin per librat e fshire
		header('Location: books.php?splash=dropbook');
		exit;
	}
	else {
		$ERROR['dropbooks'] = "agree";
	}

		
	// Defino te dhenat per cdo identifikator
	foreach ($_POST['checkbox'] as $key) {

		$BOOK = mysql_fetch_assoc(mysql_query("SELECT id,sid,title,counter,access FROM library WHERE id = '".$key."'"));
		$BOOK['comments'] = mysql_num_rows( mysql_query("SELECT id FROM comments WHERE book_id='".$BOOK['id']."'"));
		$BOOK['rating'] = ratingValue($BOOK['id']);
		$BOOK['section'] = $SECTION[$BOOK['sid']];
		$BOOK['access'] = $ACCESS[$BOOK['access']];
		$DROP[] = $BOOK;
	}

	// DROP vlerat
	$tmp->setContent("FORM",$DROP);
	
	// Convert POST and ERROR vars in Form
	$tmp->setForm($_POST,$ERROR);
	
	// LABELS
	$tmp->setLabels();
	
	// HELP
	$tmp->setContent("HELP",$tmp->xmlContent("dropbook","help.xml"));
	
	
	// Dhe cdo gje ka nje fund, trego faqen						  
	echo $tmp->vorlage;

?>